package com.example.lhh.user.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.lhh.common.LhhResponseJson;
import com.example.lhh.jwt.LhhJwtToke;
import com.example.lhh.jwt.LhhJwtUtil;
import com.example.lhh.user.entity.LhhUser;
import com.example.lhh.user.service.ILhhUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luoHongHu
 * @since 2025-03-25
 */
@RestController
@CrossOrigin/*解决跨域*/
@RequestMapping("/lhhUser")
public class LhhUserController {

    @Autowired
    ILhhUserService iLhhUserService;

    /**
     * 登录
     * @param lhhUser 用户
     * @return
     */
    @PostMapping(value = "/login")
    @ApiOperation("登录")
    public LhhResponseJson login(@RequestBody LhhUser lhhUser){
        if(lhhUser == null){
            return new LhhResponseJson(false,"用户名和密码不能为空");
        }
        if(StringUtils.isEmpty(lhhUser.getUserNo()) || StringUtils.isEmpty(lhhUser.getPassWord())){
            return new LhhResponseJson(false,"用户名或者密码不能为空");
        }
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //通过jwt创建toke
        String jwtToken = LhhJwtUtil.createToken(lhhUser.getUserNo(),null,null);
        LhhJwtToke token = new LhhJwtToke(jwtToken,lhhUser.getPassWord());
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            return new LhhResponseJson("401",false,"账户不存在");
        }catch (IncorrectCredentialsException e) {
            return new LhhResponseJson("401",false,"密码错误");
        }

        LhhUser user =  iLhhUserService.getLhhUserByUserNo(lhhUser.getUserNo());
        user.setPassWord("");
        user.setSalt("");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("user",user);
        jsonObject.put("token",token);
        return new LhhResponseJson("200",true,"登录成功",jsonObject);
    }

    /**
     * 注册
     * @param userNo 密码
     * @param password 用户
     * @return map
     */
    @ApiOperation("注册")
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public LhhResponseJson register(@Param("userNo") String userNo, @Param("password") String password) {
        Map<String,Object> map = new HashMap<>();
        if(StringUtils.isEmpty(userNo) || StringUtils.isEmpty(password)){
            return new LhhResponseJson(false,"用户名或者密码不能为空");
        }
        JSONObject jsonObject = iLhhUserService.register(userNo,password);
        if(!jsonObject.getBoolean("success")){
            return new LhhResponseJson(jsonObject.getBoolean("success"),jsonObject.get("msg").toString());
        }
        return new LhhResponseJson(true,"注册成功");
    }

    /**
     * 获取用户拥有的菜单
     * @param userName
     * @return
     */
    @ApiOperation("获取用户权限菜单")
    @GetMapping("/getUserMenu")
    public LhhResponseJson getUserMenu(String userName){
        List<Map<String,Object>> lhhPermissionList = null;
        try{
            lhhPermissionList = iLhhUserService.getUserMenu(userName);
            return new LhhResponseJson("200",true,lhhPermissionList);
        }catch (Exception e){
            e.printStackTrace();
            return new LhhResponseJson("400",false,lhhPermissionList);
        }
    }

    /**
     * 退出系統，则清除存在缓存中中的会话信息
     * @return
     */
    @GetMapping("/logout")
    public LhhResponseJson logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new LhhResponseJson("200",true,null);
    }
}